package com.dpteam.service.impl;

import com.dpteam.bean.ImportInfo;
import com.dpteam.bean.UserInfo;
import com.dpteam.dao.ImportProductDao;
import com.dpteam.dao.ProductDao;
import com.dpteam.dao.UserDao;
import com.dpteam.model.ImportProduct;
import com.dpteam.model.Product;
import com.dpteam.model.User;
import com.dpteam.service.StorageService;
import com.dpteam.util.Constant;
import com.dpteam.util.Page;
import com.dpteam.util.TransformUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xwh on 2016/1/11.
 */
@Service
@Transactional
public class StorageServiceImpl implements StorageService {

    @Resource
    private ImportProductDao importProductDao;

    @Resource
    private ProductDao productDao;

    @Resource
    private UserDao userDao;

    private static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);


    @Override
    public boolean addImportProduct(String barcode, int amount) {
        logger.info("添加入库[barcode:{},amount:{},userId:{}]", barcode, amount);
        Product product = productDao.findByBarcode(barcode);
        if (product == null) {
            return false;
        }
        Subject subject = SecurityUtils.getSubject();
        UserInfo userInfo = (UserInfo) subject.getSession().getAttribute(Constant.currentUserKey);
        User user = userDao.get(User.class, userInfo.getId());
        if (user == null) {
            return false;
        }
        LocalDateTime updateTime = LocalDateTime.now();
        float total = product.getBuyPrice() * amount;
        ImportProduct importProduct = new ImportProduct();
        importProduct.setProduct(product);
        importProduct.setAmount(amount);
        importProduct.setUser(user);
        importProduct.setUpdateTime(updateTime);
        importProduct.setTotal(total);
        importProductDao.save(importProduct);
        product.setStorage(product.getStorage() + amount);
        return true;
    }

    @Override
    public boolean updateStorage(int id, int storage) {
        logger.info("修改库存[id:{}, amount:{}]", id, storage);
        Product product = productDao.get(Product.class, id);
        if (product == null) {
            return false;
        }
        product.setStorage(storage);
        productDao.saveOrUpdate(product);
        return true;
    }

    @Override
    public List<ImportInfo> findImportInfoAllPage(Page page) {
        logger.info("findImportInfoAllPage");
        String hql = "from ImportProduct order by id";

        List<ImportProduct> importProductList = importProductDao.find(hql, page);
        List<ImportInfo> importInfos = new ArrayList<>();
        for (ImportProduct ip : importProductList) {
            importInfos.add(TransformUtil.fromImportProduct(ip));
        }
        return importInfos;
    }
}
